package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.job.deviceData.IotMsgNotifyData;
import com.zzyl.nursing.job.deviceData.IotMsgService;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.service.IDeviceDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2025/5/26 17:13
 * @Version 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService {

    private final DeviceMapper deviceMapper;
    private final RedisTemplate redisTemplate;

    @Override
    public void saveDeviceData(IotMsgNotifyData notifyData) {
        //搞清楚做做什么，就是存 上报的数据  关键点：一个属性 存一条
        log.info("开始存储设备数据");
        //查询设备表
        String deviceId = notifyData.getHeader().getDeviceId();
        Device device = deviceMapper.selectOne(Wrappers.<Device>lambdaQuery().eq(Device::getIotId, deviceId));
        if(device == null){
            log.info("没有对应的设备:{}",deviceId);
            return;
        }
        List<DeviceData> deviceDataList = new ArrayList<>();
        List<IotMsgService> services = notifyData.getBody().getServices();
        for (IotMsgService service : services) {
            LocalDateTime LdTime =  LocalDateTimeUtil.parse(service.getEventTime(), "yyyyMMdd'T'HHmmss'Z'");
            //日期时区转换
            LocalDateTime eventTime = LdTime.atZone(ZoneId.from(ZoneOffset.UTC))
                    .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
                    .toLocalDateTime();

            Map<String, Object> properties = service.getProperties();
            properties.forEach((key, value) -> {
                DeviceData deviceData = new DeviceData();
                BeanUtils.copyProperties(device,deviceData);
                deviceData.setFunctionId(key);
                deviceData.setDataValue(value.toString());
                deviceData.setAlarmTime(eventTime);
                deviceData.setId(null);   //天坑
                deviceDataList.add(deviceData);
                }
            );
        }
        try {
            super.saveBatch(deviceDataList);
            //同时存到Redis  最新数据
            redisTemplate.opsForHash().put(CacheConstants.IOT_NEW_DATA ,deviceId, deviceDataList);
        }catch (Exception e){
            log.error("插入数据异常",e);
        }
    }
}